package com.bxd.baodan.widget;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.bxd.baodan.R;
import com.bxd.baodan.adapter.MenuPopuoWindowAdapter;

import android.widget.PopupWindow.OnDismissListener;;

public class MenuPopupWindow extends PopupWindow implements OnItemClickListener, OnDismissListener{
	private View view;
	private Context context;
	private String[] titles;
	private ListView pop_list;
	private int[] iconResources;
	private ItemClickListener itemClickListener;
	private MenuPopuoWindowAdapter adapter;
	
	public MenuPopupWindow(View view, Context context, String[] titles,
			int[] iconResources) {
		super();
		this.view = view;
		this.context = context;
		this.titles = titles;
		this.iconResources = iconResources;
		
		View v = LayoutInflater.from(context).inflate(R.layout.menu_pos_list, null);
		this.setContentView(v);
		pop_list = (ListView) v.findViewById(R.id.menu_pop_list);
		
		WindowManager m = ((Activity)context).getWindowManager();
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		this.setWidth(d.getWidth() * 1/2);//LayoutParams.WRAP_CONTENT
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		this.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.menu_pop_bg));
        this.setOnDismissListener(this);
        
        adapter = new MenuPopuoWindowAdapter(iconResources, titles, context);
        pop_list.setAdapter(adapter);
        pop_list.setOnItemClickListener(this);

	}
	public ItemClickListener getItemClickListener() {
		return itemClickListener;
	}
	public void setItemClickListener(ItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}
	
    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
    	// TODO Auto-generated method stub
    	super.showAsDropDown(anchor, xoff, yoff);
    }
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		itemClickListener.onItemClick(arg2);
		this.dismiss();
	}
	
	public interface ItemClickListener {
		abstract void onItemClick(int position);
	}

	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub
		
	}
	
	
}
